package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 *  cskaoyan
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse queryAllOrders(OrderListRequest request) {

        OrderListResponse response = new OrderListResponse();

        // 配置分页
        PageHelper.startPage(request.getPage(),request.getSize());

        try {
            // 参数校验
            request.requestCheck();

            // 查询订单表
            Example orderExample = new Example(Order.class);
            orderExample.createCriteria().andEqualTo("userId",request.getUserId());
            // 设置排序
            orderExample.setOrderByClause(request.getSort());
            List<Order> orders = orderMapper.selectByExample(orderExample);
            PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
            List<Order> orderList = orderPageInfo.getList();

            List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
            for (Order order : orderList) {
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
                // 查询订单商品表
                Example orderItemExample = new Example(OrderItem.class);
                orderItemExample.createCriteria().andEqualTo("orderId",order.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                orderDetailInfo.setOrderItemDto(orderItemDtos);

                // 查询订单地址表
                Example orderShippingExample = new Example(OrderShipping.class);
                orderShippingExample.createCriteria().andEqualTo("orderId",order.getOrderId());
                List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(orderShippingExample);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShippings.get(0));
                orderDetailInfo.setOrderShippingDto(orderShippingDto);

                orderDetailInfos.add(orderDetailInfo);
            }
            OrderDetailAndTotal orderDetailAndTotal = new OrderDetailAndTotal();
            orderDetailAndTotal.setData(orderDetailInfos);
            orderDetailAndTotal.setTotal((long) orders.size());
            response.setOrderDetailAndTotal(orderDetailAndTotal);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("OrderQueryServiceImpl.queryAllOrders Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public OrderDetailResponse queryOrderDetailByOrderId(OrderDetailRequest request) {
        OrderDetailResponse response = new OrderDetailResponse();

        try {
            // 参数校验
            request.requestCheck();

            // 查询订单
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());

            // 查询订单商品
            Example example = new Example(OrderItem.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId",request.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

            // 查询订单地址
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(request.getOrderId());

            // 响应结果封装
            response.setUserName(orderShipping.getReceiverName());
            response.setOrderTotal(order.getPayment());
            response.setUserId(order.getUserId());
            response.setGoodsList(orderItemDtos);
            response.setOrderStatus(order.getStatus());
            response.setTel(orderShipping.getReceiverPhone());
            response.setStreetName(orderShipping.getReceiverAddress());
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("OrderQueryServiceImpl.queryOrderDetailByOrderId Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
